package com.hanson.filter;


import com.hanson.wapper.PageRequestWapper;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description 设置默认分页参数
 * @auther Hanson
 * @create 2020/4/13 22:09
 */
@Component
@WebFilter(urlPatterns = "/**", filterName = "pageFilter")
public class PageFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PageRequestWapper requestWapper = new PageRequestWapper((HttpServletRequest) request);
        String pageNoStr = requestWapper.getParameter("pageNo");
        String pageSizeStr = requestWapper.getParameter("pageSize");
        requestWapper.addParameter("pageNo", pageNoStr==null || Integer.parseInt(pageNoStr)<1?1:Integer.parseInt(pageNoStr));
        requestWapper.addParameter("pageSize", pageSizeStr==null || Integer.parseInt(pageSizeStr)<1?12:Integer.parseInt(pageSizeStr));
        chain.doFilter(requestWapper, response);
    }
}
